/**
 * 
 */
requirejs.config({
    baseUrl:'http://static.auoffice.com/static/js' ,
    waitSeconds: 0,
    paths: {
    	jquery:'libs/jquery.min',
    	validate:'libs/validate/jquery.validate.min',
        datatables: 'libs/dataTables/jquery.dataTables.min',
        btdatatables: 'libs/dataTables/dataTables.bootstrap.min',
        chosen:'libs/chosen/chosen.jquery',
        //plugins
        base: 'common/base',
    },
    shim: {
    	'validate': {
    		deps: ['jquery'],
    		exports: 'validate'
    	},
        'datatables': {
            deps: ['jquery'],
            exports: 'datatables'
        },
        'btdatatables': {
            deps: ['jquery'],
            exports: 'btdatatables'
        }, 
        'chosen': {
            deps: ['jquery'],
            exports: 'chosen'
        },
    }
});

require([
    'jquery',
 	'common/base',
 	'chosen',
 	'validate',
 	'datatables',
 	'btdatatables'
],function($,base,chosen) {
    var table = $("#data_table").DataTable({
        "searching": true,
        "processing": true,
        "serverSide": true,
        "ordering": false,
        "deferRender": true,
        "ajax": {
            "url": '/demand/demand/list/',
            "type": "POST"
        },
        "columnDefs": [
            {
               "targets": 0,
               "data": null,
               "name": null,
               "searchable": false,
               "bSortable": false,
               "render": function (data, type, full) {
                   var html = '<input  style="width:18px;height:18px" type="checkbox" name="demand_id" data-designee_id="'+full.designee_id+'" data-id="'+full.id+'"/>';
                   return html;
               }
            },
            {
                "targets": 1,
                "data": null,
                "name": null,
                "searchable": false,
                "bSortable": false,
                "render": function (data, type, full) {
                	var html = '<p style="overflow: hidden;text-overflow: ellipsis;display: -webkit-box;-webkit-line-clamp: 3;-webkit-box-orient: vertical;">'+full.title+'</p>';
                	return html;
                }
            },
            {
            	"targets": 2,
            	"data": null,
            	"name": null,
            	"searchable": false,
            	"bSortable": false,
                "render": function (data, type, full) {
                    var html = '<p style="overflow: hidden;text-overflow: ellipsis;display: -webkit-box;-webkit-line-clamp: 3;-webkit-box-orient: vertical;">'+full.content+'</p>';
                    return html;
                }
            },
            {
            	"targets": 3,
            	"data": "create_user_name",
            	"name": "create_user_name",
            	"searchable": false,
            	"bSortable": false
            },
            {
            	"targets": 4,
            	"data": "create_time",
            	"name": "create_time",
            	"searchable": false,
            	"bSortable": false
            },
            {
            	"targets": 5,
            	"data": "designee_name",
            	"name": "designee_name",
            	"searchable": false,
            	"bSortable": false
            },
            {
            	"targets": 6,
            	"data": "designee_time",
            	"name": "designee_time",
            	"searchable": false,
            	"bSortable": false
            },
            {
            	"targets": 7,
            	"data": "status_desc",
            	"name": "status_desc",
            	"searchable": false,
            	"bSortable": false
            },
        ],
        "initComplete": function () {
            var api = this.api();
            var column = api.column(7);
            var select = $('<select class="form-control" id="_status"><option value="">--状态--</option></select>')
            .appendTo($(column.header()).empty() )
            .on( 'change', function (){
                var val = $.fn.dataTable.util.escapeRegex(
                    $(this).val()
                );
                $("#all").prop("checked",false);
                column.search( val ? '^'+val+'$' : '', true, false ).draw();
            });
            select.append(_status());
        },
        "oLanguage": {
            "sLengthMenu": '每页显示 _MENU_条 &nbsp;',
            "sZeroRecords": "没有找到符合条件的数",
            "sProcessing": "加载中...",
            "sInfo": "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
            "sInfoEmpty": "木有记录",
            "sInfoFiltered": "(从 _MAX_ 条记录中过滤)",
            "sSearch": "搜索：",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "«",
                "sNext": "»",
                "sLast": "尾页"
            }
        }
    });
    
    var _status = function() {
        return  '<option value="0">已提交</option><option value="1">驳回</option><option value="2">审核通过</option><option value="3">进行中</option><option value="99">已关闭</option>';
    };  
  //datatables 行点击事件
    $("#data_table tbody").on('click','tr', function () {
    	var checked = $("td",this).find("input[type=checkbox]");
    	if ($(checked).is(':checked')){
    		$(checked).prop("checked",false);
    	}else{
    		$(checked).prop("checked",true);
    	}
    	var tr_len = $("#data_table tbody tr").find("input[type=checkbox]").length;
    	var check_len = $("#data_table tbody tr").find("input[type=checkbox]:checked").length;
    	if (tr_len == check_len){
    		$("#all").prop("checked",true);
    	}else{
    		$("#all").prop("checked",false);
    	}
    });
    
    //datatables 全选事件
    $("#all").click(function(){
    	if ($(this).is(':checked')){
    		$("#data_table tbody tr").find("input[type=checkbox]").prop("checked",true);
    	}else{
    		$("#data_table tbody tr").find("input[type=checkbox]").prop("checked",false);
    	}
    });
  //点击添加需求按钮
    $(".add").click(function(){
    	var o = "/demand/demand/add/";
    	var l = "新增需求";
    	base.contabs.addtab(o,l,window.document.location.pathname);
    });
    //修改需求
    $(".update").click(function(){
    	
    	var len = $("[name=demand_id]:checked").length;
    	if (len != 1){
    		base.layui.alert("信息","请选择一行数据编辑");
    		return;
    	}
    	var demand_id = $("[name=demand_id]:checked").attr("data-id");
    	var o = "/demand/demand/edit/?demand_id="+demand_id;
    	var l = "修改需求";
    	base.contabs.addtab(o,l,window.document.location.pathname);
    });
	
	var callback = function(){
		if (parent_layer){
			layer.close(parent_layer);
		}
		table.draw();
	}
});